<template>
  <div>
    <!-- 搜索 -->
    <TopFilter
      ref="commonTopFilter"
      :list="topFilterInit"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      @resetChange="resetHandle"
    ></TopFilter>
    <!-- 按钮 -->

    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'records'"
      columnsRef="notification_maintain_index_01"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <multiuse-button type="primary" :click="exportFileBtn.bind(this)">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleAdd" :title="title" destroyOnClose width="60%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detailed :row = 'row' @close="closeAdd" />
    </Drawer>
    <BaseDialog :visible.sync="selectDelearFlag" title="服务中心选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave" selectionType="multiple" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { getTransferInfo,confirmSync,getListForExport} from '@/api/customerRepair/vehicleOwnerManagement/vehicleTransfer/index';
import detailed from './detailed';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
export default {
  name: "vehicleTransfer",
  mixins: [dictionary],
  components: {
    DealerList,
    detailed
  },
  data() {
    return {
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchApi: getTransferInfo, //api 接口 table请求数据入口
      fetchParams: {},
      visibleAdd:false,
      selectDelearFlag: false,
      selectDealerData: [],
      searchDealerCode:[],
      dealerData: [],
      title:'',
      row:{}
    };
  },
  methods: {
    topFilterInitFun() {
      return [
        {type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: ''},
        {
          type: 'INPUT',
          label: '车辆过户经销商',
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => this.selectDelearFlag = true} />
              </div>
            );
          },
        },
        {type: 'RANGE_DATE', label: '车辆过户日期', fieldName: 'transferDateFrom|transferDateTo', placeholder: '请输入', initialValue: ''}
      ];
    },
    creatHeader() {
      return [

        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 120,
          sorter: true,
          fixed: 'left',
          render: props => {
            let examine = props.row.isConfirm == '是' ? true : false;
            return (
              <div>
                <el-button size="mini" type="text"  onClick={() => this.confirmSync(props.row)} disabled={examine}>
                  确认同步
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text"  onClick={() => this.examine(props.row) }>
                  查看
                </el-button>
              </div>
            );
          }
        },

        {title: '是否确认', dataIndex: 'isConfirm', sorter: true},
        {title: 'VIN', dataIndex: 'vin', sorter: true},
        {title: '车牌号', dataIndex: 'license', sorter: true},
        {title: '原车主名称', dataIndex: 'oldOwnerName', sorter: true},
        {title: '原车主手机', dataIndex: 'oldMobile', sorter: true},
        {title: '新车主名称', dataIndex: 'newOwnerName', sorter: true},
        {title: '新车主手机', dataIndex: 'newMobile', sorter: true},
        {title: '过户经销商', dataIndex: 'dealerName', sorter: true},
        {title: '过户日期', dataIndex: 'transferDate', sorter: true,dateFormat: 'yyyy-MM-dd HH:mm'},
        {title: '确认同步日期', dataIndex: 'confirmAt', sorter: true,dateFormat: 'yyyy-MM-dd HH:mm'},
        {title: '确认人', dataIndex: 'confirmBy', sorter: true}
      ];
    },
    changeHandle(val) {
      console.log("val",val)
      this.fetchParams = Object.assign({},val,{dealerCompanyCode: this.searchDealerCode.length ? this.searchDealerCode : null});
    },
    Delearclose() {
      this.selectDelearFlag = false;
    },
    Delearsave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach(item => {
        this.selectDealerData.push(item.dealerCompanyCode);
        this.searchDealerCode.push(item.dealerCode);
      });
      this.selectDelearFlag = false;
    },
    examine(row) {
      this.title ='车辆过户详情';
      this.row = row;
      this.visibleAdd = true;
    },
    //导出
    async exportFileBtn() {
      let params = { ...this.fetchParams};
      console.log(params, '导出::');
      let res = await getListForExport(params);
      console.log(res.data, '导出::');
      try {
        exportExcelFile(res.data, '车辆过户信息');
      } catch (e) {
        console.log(e, '错误::');
      }
    },
    async confirmSync(row) {
      let data = Object.assign({}, row);
      await this.$confirm('是否确认同步', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await confirmSync(data);
          if (res.resultCode == 200) {
            this.$notify.success({title: '提示信息', message: '操作成功!'});
            this.fetchParams = {...this.fetchParams};
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消确认'
          });
        });
    },
    closeAdd(rData) {
      this.visibleAdd = false;
      if (rData.type === 'refresh') {
        // 重新加载
        this.fetchParams = {...this.fetchParams};
      }
    },
    resetHandle(val) {
      this.dealerData = [];
      this.selectDealerData = [];
      this.searchDealerCode=[];
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCompanyCode: null }, { xhrAbort: false });
    },
    collapseHandle() {

    }
  }
}
</script>

<style scoped>

</style>
